﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KPIS.GERP.SERVICE.Model;
using System.Globalization;

namespace KPIS.GERP.SERVICE.WorkflowService.Class
{
    public class Utility
    {        
        public int GetRegularWorkingDays(DateTime start, DateTime end)
        {
            // ตรวจสอบ เสาร์ อาทิตย์ และวันหยุด .Where(x => CheckHoliday(x.ToShortDateString()) == 0)//
            return Range(start, end)
                .Where(x => x.DayOfWeek != DayOfWeek.Sunday)
                .Where(x => x.DayOfWeek != DayOfWeek.Saturday)
                .Where(x => CheckHoliday(x.ToShortDateString()) == 0)
                .Count();
         }

        public int GetRegularAllDays(DateTime start, DateTime end)
        {
            return (
                from day in Range(start, end)                
                select day).Count();
        }

        //public static int cnt;
        static List<hrm_ps_non_working_days> lst;
        private int CheckHoliday(string d)
        {   
            if (lst == null)
            {
                lst = new DAL.NonWorkingDays().GetAllNonWorkingDays(
                    (new ThaiBuddhistCalendar().GetYear(DateTime.Now)).ToString());
            }
         
            var c = from item in lst
                    where item.non_working_date.ToShortDateString().Equals(d)
                    select lst;
            return c.ToList().Count();
        }
        
        IEnumerable<DateTime> Range(DateTime start, DateTime end)
        {
            if (start <= end)
            {
                while (start <= end)
                {
                    yield return start;
                    start = start.AddDays(1);
                }
            }
            else
            {
                while (end <= start)
                {
                    yield return start;
                    start = start.AddDays(-1);
                }
            }
        }

    }
}